package com.yiwei.zxp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.MyAddressAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.AddressAddBean;
import com.yiwei.zxp.api.bean.IdBean;
import com.yiwei.zxp.api.bean.SmsSendBean;
import com.yiwei.zxp.api.model.AddressListsModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.event.AddAddressEvent;
import com.yiwei.zxp.event.ChangeAddressEvent;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyAddressActivity extends BaseActivity implements MyAddressAdapter.OnChooseListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_add)
    TextView btnAdd;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;

    private Unbinder unbiner;
    private MyAddressAdapter adapter;
    private boolean isSelectAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_address);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        isSelectAddress = getIntent().getBooleanExtra("is_select", false);
        txtTitle.setText("我的地址");
        adapter = new MyAddressAdapter(this);
        adapter.setOnChooseListener(this);
        list.setAdapter(adapter);
    }

    public void getAddressList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        Api.api_service.getAddressLists().compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.update(result.datas);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    public void getAddressDelete(String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        IdBean bean = new IdBean();
        bean.id = id;
        Api.api_service.getAddressDelete(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "删除成功");
                    getAddressList();
                });
    }

    public void getAddressAdd(AddressAddBean item) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        Api.api_service.getAddressAdd(item).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                    getAddressList();
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddressList();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_add:
                Util.startActivity(this, AddressEditActivity.class);
                break;
        }
    }

    @Override
    public void onSelect(AddressListsModel.DatasBean bean) {
        if (isSelectAddress) {
            EventBus.getDefault().post(new ChangeAddressEvent(bean));
            finish();
        }
    }

    @Override
    public void onDelete(String id) {
        Dialog.showDefaultDialog(this, "", "确定删除该地址吗？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {
                getAddressDelete(id);
            }

            @Override
            public void cancel() {

            }
        });
    }

    @Override
    public void onEdit(AddressListsModel.DatasBean item) {
        Intent intent = new Intent(this, AddressEditActivity.class);
        intent.putExtra("data", item);
        startActivity(intent);
        overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    @Override
    public void onDefault(AddressListsModel.DatasBean addressdata) {
        AddressAddBean submitData = new AddressAddBean();
        submitData.province_id = addressdata.province_id;
        submitData.city_id = addressdata.city_id;
        submitData.district_id = addressdata.district_id;
        submitData.id = addressdata.id;
        submitData.address = addressdata.address;
        submitData.name = addressdata.name;
        submitData.phone = addressdata.phone;
        submitData.is_default = "1";
        getAddressAdd(submitData);
    }
}
